package org.agile2soft.nexoma.tapestry.encoder;

import org.agile2soft.nexoma.controller.ExpedientTypePhaseController;
import org.agile2soft.nexoma.entity.ExpedientTypePhase;
import org.agile2soft.nexoma.entity.ExpedientTypePhaseId;
import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.ValueEncoder;
import org.hibernate.SessionFactory;

import br.com.arsmachina.module.service.PrimaryKeyTypeService;
import br.com.arsmachina.tapestrycrud.encoder.Encoder;
import br.com.arsmachina.tapestrycrud.hibernate.encoder.HibernatePrimaryKeyEncoder;

public class ExpedientTypePhaseEncoder
       extends HibernatePrimaryKeyEncoder<ExpedientTypePhase,ExpedientTypePhaseId>
       implements Encoder<ExpedientTypePhase,ExpedientTypePhaseId> {

    public ExpedientTypePhaseEncoder(Class<ExpedientTypePhase> entityClass,
            ExpedientTypePhaseController controller,
            SessionFactory sessionFactory) {
        super(entityClass, controller, sessionFactory);
    }

//    public ExpedientTypePhaseEncoder(Class<ExpedientTypePhase> entityClass,
//            ExpedientTypePhaseController controller,
//            SessionFactory sessionFactory,
//            PrimaryKeyTypeService primaryKeyTypeService) {
//        super(entityClass, controller, sessionFactory,primaryKeyTypeService);
//    }

    public Class<ExpedientTypePhaseId> getKeyType() {
        return ExpedientTypePhaseId.class;
    }

    public String toClient(ExpedientTypePhase expedientTypePhase) {
        return String.format("%s-%s",expedientTypePhase.getId().getPhaseId()
                                    ,expedientTypePhase.getId().getExpedientTypeId());
    }

    public ExpedientTypePhase toValue(String clientValue) {
        String[] params = clientValue.split("-");
        final Integer phaseId = new Integer(params[0]);
        final Integer expedientTypeId = new Integer(params[1]);
        return getController().findById(new ExpedientTypePhaseId(phaseId,expedientTypeId));
    }

    public ValueEncoder<ExpedientTypePhase> create(Class<ExpedientTypePhase> type) {
        return this;
    }

    public Object toActivationContext(ExpedientTypePhase arg0) {
        return null;
    }

    public ExpedientTypePhase toObject(EventContext arg0) {
        return null;
    }

    public String toLabel(ExpedientTypePhase expedientTypePhase) {
        return String.format("ExpedientTypePhase - %s",expedientTypePhase.getId());
    }

}
